<template>
  <el-dialog
    :visible.sync="visible"
    :title="title"
    width="400px"
    destroy-on-close
  >
    <el-form
      size="small"
      :model="formData"
      label-position="right"
      label-width="100px"
      :rules="rules"
      ref="formRef"
    >
      <el-row :offset="20">
        <el-col :span="24">
          <el-form-item prop="shopName" label="店铺">
            <el-input
              v-model.trim="formData.shopName"
              style="width: 100%"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="shopType" label="店铺类型">
            <el-select v-model.trim="formData.shopType" style="width: 100%">
              <el-option
                v-for="item in dict.SHOPTYPE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="footer_box">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { addHandle, editHandle } from '@/api/mallManage/index.js'
export default {
  name: 'AddOrEditDialog',
  dicts: ['SHOPTYPE'],
  props: {
    type: String,
    successAfter: Function,
  },
  data() {
    return {
      visible: false,
      loading: false,
      title: '新增',
      formData: {
        shopName: undefined,
        shopType: '0',
      },
      rules: {
        shopName: [
          { required: true, message: '请输入店铺名称', trigger: 'change' },
        ],
        shopType: [
          { required: true, message: '请选择店铺类型', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    handleOpen(type, row) {
      this.visible = true
      if (type === 'add') {
        this.title = '新增'
        this.formData = {
          shopName: undefined,
          shopType: undefined,
        }
      } else {
        this.title = '编辑'
        this.formData = row
      }
    },
    async handleSubmit() {
      await this.$refs.formRef.validate()
      this.loading = true
      let apiFun = addHandle
      if (this.title === '编辑') {
        apiFun = editHandle
      }
      apiFun(this.formData)
        .then((data) => {
          this.$notify.success(data)
          this.successAfter && this.successAfter()
          this.visible = false
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.footer_box {
  text-align: right;
}
</style>
